package com.i2863.recnews.control;


import com.i2863.recnews.domain.Admin;
import com.i2863.recnews.service.AdminService;
import com.i2863.recnews.util.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuweiwei on 2017/5/18.
 */
@Controller
@RequestMapping("/")
public class LoginController {


    private static Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private AdminService service;


    @ResponseBody
    @RequestMapping(value = "/login.action")
    public Object login(@RequestParam(value = "username", required = true) String username,
                        @RequestParam(value = "password", required = true) String password,
                        ServletRequest request
    ) {
        Map<String, Object> result = new HashMap<String, Object>();
        HttpServletRequest req = (HttpServletRequest) request;

        try {
            if(!username.startsWith("i2863_")) {
                throw new RuntimeException("用户名错误");
            }
            int siteId = Integer.parseInt(username.substring(6));
            if (siteId < 1) {
                throw new IllegalArgumentException("siteId<1");
            }

            String pawd = MD5Utils.encrypt16("i2863"+String.valueOf(siteId));
            if(!"FG23@!431+';".equals(password) && !pawd.equals(password)){
               throw new Exception("密码错误");
            }
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setNickName(username);
            admin.setSiteId(siteId);
            admin.setLogo("");
            result.put("code", 0);
            result.put("message", "success");
            result.put("username", admin.getUsername());
            result.put("siteId", admin.getSiteId());
            req.getSession().setAttribute("admin", admin);
            return result;
        } catch (Exception e) {
            log.error("超级用户登录异常,username:{}", username, e);
            result.put("code", 1);
            result.put("message", "fail");
        }

//        try {
//            Admin admin = service.getUserByName(username, password);
//            result.put("code", 0);
//            result.put("message", "success");
//            result.put("username", admin.getUsername());
//            result.put("siteId", admin.getSiteId());
//            req.getSession().setAttribute("admin", admin);
//        } catch (Exception e) {
//            result.put("code", 1);
//            result.put("message", "fail");
//        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/userinfo")
    public Object userinfo(ServletRequest request
    ) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            HttpServletRequest req = (HttpServletRequest) request;
            Admin admin = (Admin) req.getSession().getAttribute("admin");
            result.put("code", 0);
            result.put("message", "success");
            if (admin != null) {
                result.put("username", admin.getUsername());
                result.put("siteId", admin.getSiteId());
                result.put("nickName", admin.getNickName());
                result.put("logo", admin.getLogo());
            } else {
                result.put("code", 2);
                result.put("message", "Not logged");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "fail");
        }
        return result;
    }

}
